<script setup>
import { defineEmits, ref,onMounted } from 'vue'
import {saveApi, updateApi} from "../../../api/course.js";
import {ElMessage} from "element-plus";
import {useCourseStore} from "../../../stores/course";
//定义事件--父级
const emit = defineEmits(['flush']);

const store=useCourseStore()

// 表单数据
const formData = ref({
  id: 0,
  ctId:"",
  imgUrl:"",
  info:"",
  score:"",
  teacher:"",
  teacherInfo:"",
  times:"",
  title:""
})
// 表单验证
const formRules = ref({
  title: [
    { required: true, message: '请输入名称', trigger: 'blur' },
    { min: 2, max: 20, message: '请输入2~20个字符', trigger: 'blur' }
  ]
})
const imageUrl=ref("")
const handleAvatarSuccess=(res, file)=>{
  imageUrl.value = URL.createObjectURL(file.raw);
  formData.value.imgUrl=res.data;
}
// 提交验证
const formRef = ref()
const onSubmit = () => {
  formRef.value.validate(async (isSuccess) => {
    if (isSuccess) {
      let r;
      const isadd=formData.value.id==0||formData.value.id==undefined;
      if(isadd) {
        r=await saveApi(formData.value);
      }else {
        r=await updateApi(formData.value);
      }
      if(r.code==0){
        ElMessage({
          type: 'success',
          message: isadd?'新增课程成功':'修改课程成功'})
        state.value=false
        emit('flush')
      }else {
        ElMessage.error(r.data)
      }
    }
  })
}
// 对话框状态
const state = ref(false)

// 暴露
defineExpose({
  state,
  formData
})
</script>
<template>
  <el-dialog
    v-model="state"
    :title="`${formData.id>0? '修改' : '添加'} 课程`"
    width="600"
    :before-close="() => (state = false)"
  >
    <!-- 内容 -->
    <el-form
      v-if="state"
      size="large"
      label-width="80"
      :model="formData"
      :rules="formRules"
      ref="formRef"
    >
      <el-row>
        <el-col :span="12">
          <el-form-item label="课程名称" prop="title">
            <el-input placeholder="请输入课程名称" v-model="formData.title" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="课程类型" prop="ctid">
            <el-select placeholder="请选择课程类型" v-model="formData.ctId">
              <el-option v-for="t in store.types" :label="t.name" :value="t.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="课程积分">
            <el-input type="number" placeholder="请输入课程积分" v-model="formData.score" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="学习时长">
            <el-input placeholder="请输入学习时长" v-model="formData.times" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="老师姓名">
            <el-input placeholder="请输入老师姓名" v-model="formData.teacher" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="老师简介">
            <el-input type="textarea" :rows="3" placeholder="请输入老师简介" v-model="formData.teacherInfo" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="封面图片">
        <el-upload
            class="avatar-uploader"
            action="http://localhost:8080/api/resource/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
        >
          <img v-if="imageUrl" :src="imageUrl" class="dvimg" />
          <el-icon v-else class="avatar-uploader-icon">+</el-icon>
        </el-upload>
      </el-form-item>

      <el-form-item label="课程简介">
        <el-input  type="textarea" :rows="3" v-model="formData.info"/>
      </el-form-item>

      <el-form-item label="">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button type="default" @click="state = false">取消</el-button>
      </el-form-item>
    </el-form>
    <!-- 内容 -->
  </el-dialog>
</template>

<style>
.dvimg{
  width: 100px;
  height: 100px;
}
</style>
